﻿using HelloOne.OcelotSvc.DBLib;
using HelloOne.Repositories;
using HelloOne.Repositories.EF;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace HelloOne.OcelotSvc
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            Configuration = configuration;
            Env = env;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            IOC(services);
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            if (Env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }


        private void IOC(IServiceCollection services)
        {
            //services.AddDbContext<OcelotSvcDBContext>(x => x.UseSqlServer(Configuration.GetConnectionString(Env.EnvironmentName)));
            services.AddDbContext<OcelotSvcDBContext>(x => x.UseInMemoryDatabase($"OcelotSvcDB_{ Env.EnvironmentName}"));
            services.AddTransient<DbContext, OcelotSvcDBContext>();
            services.AddTransient<IRepositoryProvider, EFRepositoryProvider>();
        }

        #region 属性
        public IHostingEnvironment Env { get; }
        public IConfiguration Configuration { get; }
        #endregion
    }
}
